package com.tucanstubs.android_wheresmystuff.database;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.tucanstubs.android_wheresmystuff.items.Item;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
/**
 * This class holds all the methods needed to do searches in our database
 * based off of different constraints
 * @author Group 37
 * @version 2.0 4/12/2013
 */
public class SearchHelper {
	private static DatabaseOperator dbhelper;
	private static SQLiteDatabase database;
	
	private static String TAG = "SEARCHHELPER";
	/**
	 * Open a writable database instance. Also refresh the current 
	 * DatabaseOperator instance if it is null;
	 * @param context The current running Activity
	 */
	private static void open(Context context){
		if(dbhelper == null)
			dbhelper = DatabaseOperator.getInstance(context);
		database = dbhelper.getWritableDatabase();
	}
	/**
	 * Searches items by the given fields in searchQuery. The first element being 
	 * the name ofthe item to search, the second being the category, the third and fourth 
	 * being start and end dates and the last element being the username of the user doing the 
	 * search
	 * @param context The current running Activity
	 * @param searchQuery The fields that we are searching by
	 * @return A list of items matching our search
	 */
	public static List<Item> searchItems(Context context,List<String> searchQuery){ 
		ArrayList<Item> matchedItems = new ArrayList<Item>();
		String name = "'%" + searchQuery.get(0) + "%'";
		String category = "'" + searchQuery.get(1) + "'";
		String startDate = searchQuery.get(2);
		String endDate = searchQuery.get(3);
		String userName = "'" + searchQuery.get(4) + "'";
		String query = "SELECT  " + DatabaseOperator.ITEM_ID  + " FROM " + DatabaseOperator.ITEM_TABLE_NAME 
				+ " WHERE " + DatabaseOperator.NAME_COLUMN +" LIKE "+ name + " AND "
				+ DatabaseOperator.ITEMTYPE_COLUMN + "=" + category + " AND (" + DatabaseOperator.DATE_COLUMN + " >= " 
				+ "datetime('" + startDate + "') AND "
				+ DatabaseOperator.DATE_COLUMN + "<=" + "datetime('" + endDate + "')) AND "
				+ DatabaseOperator.USER_ID + " != " + userName;
		Log.i(TAG,query);
		open(context);
		Cursor cursor = database.rawQuery(query,null);
		matchedItems = cursorToList(context,cursor);
		database.close();
		Log.i(TAG,matchedItems.toString());
		return matchedItems;
	}
	/**
	 * Gets all the items by id based on the ids supplied in itemIds
	 * @param context The current running Activity
	 * @param itemIds A list of item ids to get items by
	 * @return The items which correspond to the itemIds
	 */
	public static List<Item> getItemsById(Context context,int[] itemIds){
		ArrayList<Item> matchedItems = new ArrayList<Item>();
		String query;
		Cursor cursor;
		open(context);
		for(int i = 0;i<itemIds.length;i++){
			query = "SELECT  " + DatabaseOperator.ITEM_ID  + " FROM " + DatabaseOperator.ITEM_TABLE_NAME 
					+ " WHERE " + DatabaseOperator.ITEM_ID + "=" + itemIds[i];
			cursor = database.rawQuery(query,null);
			cursor.moveToFirst();
			matchedItems.add(ItemHelper.cursorToItem(cursor));
			cursor.close();
		}
		database.close();
		return matchedItems;
	}
	/**
	 * Convert the cursor returned by a database query to a list of items.
	 * @param context The current running activity
	 * @param cursor The cursor returned by a query
	 * @return The list of items associated with the given cursor
	 */
	private static ArrayList<Item> cursorToList(Context context, Cursor cursor){
		ArrayList<Item> items = new ArrayList<Item>();
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			items.add(ItemHelper.getItem(context,cursor.getInt(0)));
			cursor.moveToNext();
		}
		cursor.close();
		return items;
	}
}
